package com.springboot.film.controller;

import com.springboot.film.entity.Page;
import com.springboot.film.entity.userinfo;
import com.springboot.film.service.userinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
public class userinfoCtrl {

    @Autowired
    private userinfoService userinfoService;

    //分页显示全部
    @RequestMapping("/houtai/userinfo")
    public String userinfo(Model model, String pageNow, String nosearch) {
        int totalCount = 0;
        totalCount = userinfoService.getCount();
        List<userinfo> userinfoList=new ArrayList<userinfo>();
            Page page=null;
            if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            userinfoList=userinfoService.findByPage(page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            userinfoList=userinfoService.findByPage(page.getStartPos(), page.getPageSize());
        }
        model.addAttribute("page", page);
        model.addAttribute("top", "top_userinfo");
        model.addAttribute("show_left", "left_userinfo");
        model.addAttribute("left", "left_y_userinfo");
        model.addAttribute("mypage", "userinfo");
        model.addAttribute("userinfo", userinfoList);
        if(nosearch != null){
            model.addAttribute("nosearch", nosearch);
        }
        return "houtai_userinfo";
    }

    //分页显示全部搜索
    @RequestMapping("/houtai/searchuserinfo")
    public String searchuserinfo(Model model, String pageNow,int types,String keyword) {
        int totalCount = 0;
        String keyword_new="a.name";
        if(types == 1) {
            keyword_new = "a.name";
        }else if (types == 2) {
            keyword_new = "a.email";
        }
        totalCount = userinfoService.getsearchCount(keyword_new,keyword);
        List<userinfo> userinfoList=new ArrayList<userinfo>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            userinfoList=userinfoService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            userinfoList=userinfoService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }
        if(userinfoList != null && totalCount != 0){
            model.addAttribute("page", page);
            model.addAttribute("top", "top_userinfo");
            model.addAttribute("show_left", "left_userinfo");
            model.addAttribute("left", "left_y_userinfo");
            model.addAttribute("mypage", "search&types="+types+"&keyword="+keyword+"");
            model.addAttribute("userinfo", userinfoList);
            return "houtai_userinfo";
        }else{
            model.addAttribute("nosearch", "nosearch");
            return "redirect:/houtai/userinfo?nosearch=nosearch";
        }
    }

    //更改
    @RequestMapping("/houtai/updateuserinfo")
    public String updateuserinfo(userinfo userinfo) {
        userinfoService.update(userinfo);
        return "redirect:/houtai/userinfo";
    }

    //删除
    @RequestMapping("/houtai/deleteuserinfo")
    public String deleteuserinfo(int userid) {
        userinfoService.delete(userid);
        return "redirect:/houtai/userinfo";
    }

    //跳转到更改页面
    @RequestMapping("/houtai/to_updateuserinfo")
    public String to_updateuserinfo(Model model, int userid) {
        List<userinfo> userinfoList=new ArrayList<userinfo>();
        userinfoList = userinfoService.getUserone(userid);
        model.addAttribute("top", "top_userinfo");
        model.addAttribute("show_left", "left_updateuserinfo");
        model.addAttribute("left", "left_y_userinfo");
        model.addAttribute("mypage", "userinfo");
        model.addAttribute("userinfo", userinfoList);
        return "houtai_updateuserinfo";
    }

}
